//
//  MyProgressBarView.m
//  CountDown
//
//  Created by mmac on 11/8/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "CountDownProgressView.h"

#define START_PROGRESS_VALUE 0.5
#define ERROR_PROGRESS_VALUE 0.0

@implementation CountDownProgressView

@synthesize progress;

- (float)progress { 
    return progress; 
}

- (void)setProgress:(float)progress2 { 
    if (progress2 >= 0.0 && progress2 <= 1.0) { 
        progress = progress2; 
    }else {
		progress = ERROR_PROGRESS_VALUE;
	}
	
	// update progress view
	[self setNeedsDisplay];
} 

-(void)drawProgress {
	
    CGContextRef myContext = UIGraphicsGetCurrentContext(); 


    CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);
    CGContextFillRect (myContext, [self bounds]);
	
	float x = self.bounds.origin.x;
	float y = self.bounds.origin.y;
	float width = self.bounds.size.width * self.progress;
	float height = self.bounds.size.height;
    CGContextSetRGBFillColor (myContext, 0, 0, 1, .5);
    CGContextFillRect (myContext, CGRectMake (x, y, width, height));	

}

- (id)initWithFrame:(CGRect)frame {
	
	NSLog(@"initWithFrame (%u,%u) %u x %u", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);

	if (self = [super initWithFrame:frame]) {
        // Initialization code
		progress = START_PROGRESS_VALUE; // set default progress value
    }
	
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
	
	// NSLog(@"drawRect At (%u,%u) %u x %u", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
	
	[self drawProgress];
	
}


- (void)dealloc {
    [super dealloc];
}


@end
